﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core.Utils;
using COFE.Core;
using COFE.Core.Validater;
using NUnit.Framework;
using COFE.Core.Entry;

namespace COFE.Core.Lister
{
    public abstract class PropertyProviderBase : IPropertyProvider
    {
        #region Constructor

        public PropertyProviderBase()
        {            
            RegisterMethod(DirectoryInfoProperties.IsFileBasedFS, (p) => { return false; });
            RegisterMethod(FileSystemInfoProperties.Name, 
                (p) => { return COFEPath.GetFileName(
                    this.GetProperty(
                    FileSystemInfoProperties.FullParseName)
                    .ValueAsString); });
        }

        #endregion

        #region Methods

        public virtual void Refresh()
        {

        }

        protected void RegisterMethod(object property, Func<object, object> getMethod,
            Action<object, object> setMethod = null)
        {
            lock (_getterMethodDic)
                if (_getterMethodDic.ContainsKey(property))
                {
                    _getterMethodDic[property] = getMethod;
                    lock (_setterMethodDic)
                        if (_setterMethodDic.ContainsKey(property))
                            _setterMethodDic.Remove(property);
                }
                else _getterMethodDic.Add(property, getMethod);

            lock (_setterMethodDic)
                if (setMethod != null)
                    if (_getterMethodDic.ContainsKey(property))
                    {
                        _setterMethodDic[property] = setMethod;
                    }
                    else _setterMethodDic.Add(property, setMethod);
        }

        public virtual IEnumerable<object> GetSupportedProperty()
        {
            foreach (var prop in _getterMethodDic.Keys)
                yield return prop;
        }

        public virtual PropertySupportOptions GetPropertySupportOptions(object property)
        {
            PropertySupportOptions retVal = PropertySupportOptions.None;
            if (_getterMethodDic.ContainsKey(property))
                retVal |= PropertySupportOptions.CanWrite;

            if (_setterMethodDic.ContainsKey(property))
                retVal |= PropertySupportOptions.CanWrite;

            return retVal;
        }

        public virtual PropertyPair GetProperty(object propertyType)
        {
            lock (_getterMethodDic)
                if (_getterMethodDic.ContainsKey(propertyType))
                    return new PropertyPair(propertyType, _getterMethodDic[propertyType]( propertyType));

            throw new NotImplementedException(propertyType.ToString());
        }

        public virtual void SetProperty(object propertyType, object value)
        {
            lock (_setterMethodDic)
                if (_setterMethodDic.ContainsKey(propertyType))
                {
                    IPropertyValidaterExtension.VerifyValue(propertyType, value);
                    _setterMethodDic[propertyType](propertyType, value);
                }
        }

        #endregion

        #region Data

        private Dictionary<object, Func<object, object>>
             _getterMethodDic = new Dictionary<object, Func<object, object>>();

        private Dictionary<object, Action<object, object>>
             _setterMethodDic = new Dictionary< object, Action<object, object>>();

        #endregion

        #region Public Properties

        #endregion





    }
}
